package com.code.translate.controllers;

import com.code.translate.configs.TranslateConfig;
import com.code.translate.enums.TextTranslateToolEnum;
import com.code.translate.request.TextRequest;
import com.code.translate.response.Result;
import com.code.translate.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ggh
 * @date 2022/7/20
 */
@Slf4j
@RestController
public class TranslateTextController {
    @Autowired
    private TranslateConfig translateConfig;


    /**
     * 文本翻译接口
     *
     * @param request
     * @return
     */
    @RequestMapping("/translateText")
    public Result translateText(@RequestBody TextRequest request) {
        if (request == null || StringUtils.isEmpty(request.getSource()) || StringUtils.isEmpty(request.getType())) {
            return Result.failure(ResultCode.ERROR);
        }
        try {
            // 标记是否固定使用某个翻译工具
            TextTranslateToolEnum translateTool = TextTranslateToolEnum.parseType(request.getType());
            if (translateTool == null) {
                log.info("未找到翻译工具");
                return null;
            }

            log.info("使用：" + translateTool.getMsg());
            String result = translateTool.getTranslatePlugin().translate(request.getSource());
            return Result.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.failure(ResultCode.ERROR);
    }
}
